package com.chare.Controller;

import com.chare.pojo.Admin;
import com.chare.pojo.LoginParam;
import com.chare.pojo.RespBean;
import com.chare.service.IAdminService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@Api(value = "登录")
public class LoginController {

    @Resource
    private IAdminService adminService;

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public RespBean login(@RequestBody LoginParam loginParam, HttpServletRequest request){
        return adminService.login(loginParam.getUsername(),loginParam.getPassword(),loginParam.getCode(),request);
    }


    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出成功");
    }



    @PostMapping("/quryAdminByName")
    public Admin quryAdminByName(String name){
        return adminService.getAdminByUserName(name);
    }

    @ApiOperation(value = "根据用户名查询对象")
    @GetMapping("/admin/info")
    public  Admin getAdminInfo(Principal principal)
    {
        if(null==principal)
        {
            return null;
        }String username=principal.getName();
        Admin admin=adminService.getAdminByUserName(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return  admin;
    }

}
